/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SubstanceAlignPanel.java
 *
 * Created on 08.12.2009, 10:14:38
 */

package GUI;

import org.jfree.chart.annotations.XYBoxAnnotation;

import DATA.iDataSet;
import DATA.iDataSetParts.Peak;
import DATA.iDataSetParts.Peaklist;
import DATA.iDataSetParts.SubstanceInfo;
import GUI.iFrames.DataSourceComponents.DataSourcesInternalFrame;
import GUI.iFrames.HetmapFrameComponents.HeatmapInternalFrame;

/**
 *
 * @author Bunkowski
 */
public class AlignPanel extends javax.swing.JPanel {
	
	public static final long serialVersionUID = 1L;
	
private double xStep = 0.0025;
private double yStep = 3;
private SubstanceInfo align;
private SubstanceInfo substance;
private iDataSet idataset;
private HeatmapInternalFrame heatmapInternalFrame;
private DataSourcesInternalFrame dataSourcesInternalFrame;

    /** Creates new form SubstanceAlignPanel */
    public AlignPanel() {
        initComponents();
    }

    public void setData(DataSourcesInternalFrame dataSourcesInternalFrame,SubstanceInfo substance,HeatmapInternalFrame heatmapInternalFrame) {
        this.dataSourcesInternalFrame = dataSourcesInternalFrame;
        this.idataset = dataSourcesInternalFrame.idataset;
        this.substance = substance;
        this.heatmapInternalFrame = heatmapInternalFrame;
        this.substance = substance;

        align = new SubstanceInfo(substance.getSubstanceParameter());
        align.name += " target";

        //column 0
        alignTable.getModel().setValueAt("Name", 0, 0);
        alignTable.getModel().setValueAt("1/k0", 1, 0);
        alignTable.getModel().setValueAt("RT", 2, 0);
        alignTable.getModel().setValueAt("1/k0 Tolerance", 3, 0);
        alignTable.getModel().setValueAt("RT Tolerance", 4, 0);

        //column 1
        alignTable.getModel().setValueAt(substance.name, 0, 1);
        alignTable.getModel().setValueAt(substance.dt, 1, 1);
        alignTable.getModel().setValueAt(substance.rt, 2, 1);
        alignTable.getModel().setValueAt(substance.dtTolerance, 3, 1);
        alignTable.getModel().setValueAt(substance.rtTolerance, 4, 1);



        updateAlignSubstance();
    }

    private void updateAlignSubstance(){
        alignTable.getModel().setValueAt(align.name, 0, 2);
        alignTable.getModel().setValueAt(align.dt, 1, 2);
        alignTable.getModel().setValueAt(align.rt, 2, 2);
        alignTable.getModel().setValueAt(align.dtTolerance, 3, 2);
        alignTable.getModel().setValueAt(align.rtTolerance, 4, 2);


        heatmapInternalFrame.showAlignAnno(false);
        heatmapInternalFrame.alignAnno = new XYBoxAnnotation(align.dt - align.dtTolerance, align.rt - align.rtTolerance, align.dt + align.dtTolerance, align.rt + align.rtTolerance);
        heatmapInternalFrame.showAlignAnno(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
   private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        alignTable = new javax.swing.JTable();
        alignButton = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        rtMinus = new javax.swing.JButton();
        rtPlus = new javax.swing.JButton();
        rtUp = new javax.swing.JButton();
        rtDown = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        shrinkK0 = new javax.swing.JButton();
        expandk0 = new javax.swing.JButton();
        moveK0Left = new javax.swing.JButton();
        moveK0Right = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        alignTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Parameter", "Substance", "Align target"
            }
        ));
        jScrollPane1.setViewportView(alignTable);

        alignButton.setFont(new java.awt.Font("Tahoma", 1, 18));
        alignButton.setText("Align");
        alignButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alignButtonActionPerformed(evt);
            }
        });

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        rtMinus.setText("-");
        rtMinus.setMargin(new java.awt.Insets(0, 7, 0, 5));
        rtMinus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rtMinusActionPerformed(evt);
            }
        });

        rtPlus.setText("+");
        rtPlus.setMargin(new java.awt.Insets(0, 5, 0, 3));
        rtPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rtPlusActionPerformed(evt);
            }
        });

        rtUp.setText("up");
        rtUp.setMargin(new java.awt.Insets(2, 2, 2, 2));
        rtUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rtUpActionPerformed(evt);
            }
        });

        rtDown.setText("down");
        rtDown.setMargin(new java.awt.Insets(2, 2, 2, 2));
        rtDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rtDownActionPerformed(evt);
            }
        });

        jLabel1.setText("RT:");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rtMinus)
                    .addComponent(rtPlus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(rtUp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(rtDown)))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rtUp, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rtPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rtMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rtDown))))
                .addContainerGap())
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        shrinkK0.setText("-");
        shrinkK0.setMargin(new java.awt.Insets(0, 7, 0, 5));
        shrinkK0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                shrinkK0ActionPerformed(evt);
            }
        });

        expandk0.setText("+");
        expandk0.setMargin(new java.awt.Insets(0, 5, 0, 3));
        expandk0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                expandk0ActionPerformed(evt);
            }
        });

        moveK0Left.setText("left");
        moveK0Left.setMargin(new java.awt.Insets(2, 2, 2, 2));
        moveK0Left.setMaximumSize(new java.awt.Dimension(35, 23));
        moveK0Left.setMinimumSize(new java.awt.Dimension(35, 23));
        moveK0Left.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveK0LeftActionPerformed(evt);
            }
        });

        moveK0Right.setText("right");
        moveK0Right.setMargin(new java.awt.Insets(2, 2, 2, 2));
        moveK0Right.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveK0RightActionPerformed(evt);
            }
        });

        jLabel2.setText("1/k0:");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(shrinkK0)
                    .addComponent(expandk0))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(moveK0Right, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(moveK0Left, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(moveK0Left, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(expandk0, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(shrinkK0, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(moveK0Right)))
        );

        jLabel3.setText("to max Intensity");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(alignButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(alignButton)
                            .addComponent(jLabel3)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void alignButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alignButtonActionPerformed

     //   HashMap<String,Double> measurementAlign = new HashMap<String, Double>();
        for (Peaklist pl : idataset.peaklists.values()){
            Peak bestPeak = align.getBestHit(pl.getPeaks(),"intensity");
            if (bestPeak != null){
                double scale = substance.rt/bestPeak.rt;
                scale = Math.rint(scale*1000)/1000;
                pl.scaleRt(scale);
       //         measurementAlign.put(pl.timeStamp, scale);
            } 
        }

//        for (Measurement m : idataset.measurements){
//            if(measurementAlign.containsKey(m.measurementID)){
//                m.align = measurementAlign.get(m.measurementID)*m.align;
//                System.out.println(m.measurementID+" has been marked as align");
//            }
//        }

        dataSourcesInternalFrame.updateTable();
       // if (heatmapInternalFrame.showAllPeaklists)
        heatmapInternalFrame.showAllPeaklistsToggle();
        heatmapInternalFrame.updateHeatmap();
       
    }//GEN-LAST:event_alignButtonActionPerformed

    private void rtPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rtPlusActionPerformed
        align.rtTolerance += yStep;
        updateAlignSubstance();
}//GEN-LAST:event_rtPlusActionPerformed

    private void rtMinusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rtMinusActionPerformed
        align.rtTolerance -= yStep;
        updateAlignSubstance();
}//GEN-LAST:event_rtMinusActionPerformed

    private void rtUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rtUpActionPerformed
        align.rt += yStep;
        updateAlignSubstance();
    }//GEN-LAST:event_rtUpActionPerformed

    private void rtDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rtDownActionPerformed
        align.rt -= yStep;
        updateAlignSubstance();
    }//GEN-LAST:event_rtDownActionPerformed

    private void expandk0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_expandk0ActionPerformed
        align.dtTolerance += xStep;
        updateAlignSubstance();// TODO add your handling code here:
}//GEN-LAST:event_expandk0ActionPerformed

    private void shrinkK0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shrinkK0ActionPerformed
        align.dtTolerance -= xStep;
        updateAlignSubstance();// TODO add your handling code here:
}//GEN-LAST:event_shrinkK0ActionPerformed

    private void moveK0LeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveK0LeftActionPerformed
        align.dt -= xStep;
        updateAlignSubstance();
}//GEN-LAST:event_moveK0LeftActionPerformed

    private void moveK0RightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveK0RightActionPerformed
        align.dt += xStep;
        updateAlignSubstance();
}//GEN-LAST:event_moveK0RightActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alignButton;
    private javax.swing.JTable alignTable;
    private javax.swing.JButton expandk0;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton moveK0Left;
    private javax.swing.JButton moveK0Right;
    private javax.swing.JButton rtDown;
    private javax.swing.JButton rtMinus;
    private javax.swing.JButton rtPlus;
    private javax.swing.JButton rtUp;
    private javax.swing.JButton shrinkK0;
    // End of variables declaration//GEN-END:variables

}
